package com.one.consumer.reserve.ui.activity;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.view.base.IListView;
import com.one.common.view.widget.MyTitleBar;
import com.one.consumer.BaseListActivityC;
import com.one.consumer.reserve.model.extra.AreaExtra;
import com.one.consumer.reserve.model.item.AreaItem;
import com.one.consumer.reserve.presenter.SelectAreaPresenter;
import com.one.consumer.reserve.ui.binder.AreaBinder;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/10/30 10:20 PM
 * description ：选择维修区域
 **/
@Route(path = RouterPath.SELECT_AREA)
public class SelectAreaActivity extends BaseListActivityC<SelectAreaPresenter> implements IListView,
        AreaBinder.AreaClickListener {
    private AreaExtra mExtra = new AreaExtra();
    private AreaItem areaItem;

    @Override
    protected void initPresenter() {
        mPresenter = new SelectAreaPresenter(this, this);
    }

    @Override
    public void registerDate() {
        register(AreaItem.class, new AreaBinder(this));
    }

    @Override
    public void loadData() {
        String id = "-1";
        if (areaItem != null) {
            id = areaItem.getParts_id() + "";
        }
        mPresenter.getAreaList(id);
    }

    @Override
    public void initData() {
        super.initData();
        loadData();
    }

    @Override
    public void onAreaClick(AreaItem item) {
        mExtra.getNames().add(item.getParts_name());
        mExtra.getIds().add(item.getParts_id() + "");
        if (areaItem != null) {
            if (item.getIs_hava().equals("0")) { // 没有下级，直接返回
                Intent intent = new Intent();
                intent.putExtra(AreaExtra.getExtraName(), mExtra);
                setResult(AddServiceActivity.RESULT_AREA, intent);
                finishPage();
            } else {
                // 跳转三级页面
                RouterManager.getInstance().goForResult(this, RouterPath.SELECT_QUESTION, mExtra);
            }
        } else {
            areaItem = item;
            getMyTitleBar().setTitleText(item.getParts_name());
            doRefresh();
        }
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        String title = "选择区域";
        getMyTitleBar().setMode(MyTitleBar.Mode.BACK_TITLE)
                .setTitleText(title);

    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return new GridLayoutManager(this, 2);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == AddServiceActivity.RESULT_AREA) {
            if (data != null) {
                setResult(AddServiceActivity.RESULT_AREA, data);
                finishPage();
            }

        }
    }
}
